<?php
class Back_Model_Mapper_Utilisateurs extends Jnd_Model_Mapper_Abstract
{
    protected $_dict = array(
        "id" => "id_utilisateurs", 
        "nom" => "nom", 
        "prenom" => "prenom", 
        "mail" => "mail", 
        "dateNaissance" => "date_naissance", 
        "age" => "age", 
        "adresse" => "adresse", 
        "adresseComplement" => "adresse_complement", 
        "codePostal" => "code_postal", 
        "ville" => "ville", 
        "telephoneFixe" => "telephone_fixe", 
        "telephonePortable" => "telephone_portable", 
        "pseudo" => "pseudo", 
        "motDePasse" => "mot_de_passe", 
        "licence" => "licence", 
        "creationDate" => "creation_date", 
        "creationUtilisateur" => "creation_utilisateur", 
        "modificationDate" => "modification_date", 
        "modificationUtilisateur" => "modification_utilisateur", 
        "derniereConnexion" => "derniere_connexion", 
        
        "tailleTshirt" => "taille_tshirt", 
        "tailleShort" => "taille_short", 
        "contact1Nom" => "contact1_nom", 
        "contact1Tel" => "contact1_tel", 
        "contact1Port" => "contact1_port", 
        "contact1Mail" => "contact1_mail", 
        "contact2Nom" => "contact2_nom", 
        "contact2Tel" => "contact2_tel", 
        "contact2Port" => "contact2_port", 
        "contact2Mail" => "contact2_mail",

        "autoriserPublication" => "autoriser_publication",
    );
    protected $_dictDate = array(
        "dateNaissance" => "date_naissance", 
        "creationDate" => "creation_date", 
        "modificationDate" => "modification_date", 
        "derniereConnexion" => "derniere_connexion"
    );

    public function recuperationSalt( $pseudo )
    {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->from($table->info("name"), array(
            "salt" => "MID(" . $this->getDict("motDePasse") . ", 1, " . (Jnd_Outils::SALT_LENGTH + 1) . ")"
        ));
        $select->where($this->getDict("pseudo") . "=?", $pseudo);
        
        $stmt = $select->query();
        $result = $stmt->fetch();
        $result = $result['salt'];
        
        return $result;
    }

    public function controleCouplePseudoMail( $mail, $pseudo )
    {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->where($this->getDict("mail") . "=?", $mail);
        $select->where($this->getDict("pseudo") . "=?", $pseudo);
        
        $stmt = $select->query();
        $result = $stmt->fetch();
        if (is_array($result)) {
            return $this->unmap($result);
        } else {
            return $result;
        }
    }

    public function chargerParCouplePseudoMail( $mail, $pseudo )
    {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->where($this->getDict("mail") . "=?", $mail);
        $select->where($this->getDict("pseudo") . "=?", $pseudo);
        
        $stmt = $select->query();
        $result = $stmt->fetch();
        if (is_array($result)) {
            return $this->unmap($result);
        } else {
            return $result;
        }
    }

    public function chargerParPseudo( $pseudo )
    {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->where($this->getDict("pseudo") . "=?", $pseudo);
        
        $stmt = $select->query();
        $result = $stmt->fetch();
        if (is_array($result)) {
            return $this->unmap($result);
        } else {
            return $result;
        }
    }

    public function controleExistance( $valeurs )
    {
        $donnees = $this->getDataFromDictArray($valeurs);
        $valeurs = array_intersect_key($donnees, $valeurs);
        
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        foreach ($valeurs as $champ => $val) {
            $select->where($this->getDict($champ) . "=?", $val);
        }
        $stmt = $select->query();
        $result = $stmt->fetch();
        if ($result !== false) {
            $result = $this->unmap($result);
            return $result;
        } else {
            return false;
        }
    }

    public function chercherPourAjax( $chaine = "" )
    {
        $table = $this->_getTable();
        $select = new Zend_Db_Table_Select($table);
        $select->from($table->info(Zend_Db_Table::NAME), array(
            $this->getDict("id"), 
            $this->getDict("nom"), 
            $this->getDict("prenom"), 
            $this->getDict("pseudo")
        ));
        $select->where($this->getDict("nom") . " LIKE ?", "%$chaine%");
        $select->orWhere($this->getDict("prenom") . " LIKE ?", "%$chaine%");
        $select->orWhere($this->getDict("pseudo") . " LIKE ?", "%$chaine%");

        $stmt = $select->query();
        $resultats = $stmt->fetchAll();
        $resultats = $this->unmapArray($resultats);
        
        return $resultats;
    }
}

